﻿var Tnelab;
(function (Tnelab) {
    var WebApp;
    (function (WebApp) {
        ThisWindow.AddEventListener("SizeAbleChanged", function (args) {
            ThisWindow._sizeAble = args;
        });
        class _BorderChecker {
            _isBorder = false;
            _wmszValue = 0;
            _oldCursor = document.documentElement.style.cursor;
            async SetBorderRectCursor() {
                if (!await (ThisWindow.SizeAble))
                    return;
                let x = window.event.clientX;
                let y = window.event.clientY;
                let rect = ({ left: 0, top: 0, right: window.innerWidth, bottom: window.innerHeight });
                let borderWidth = 6;
                if (!this._isBorder)
                    this._oldCursor = document.documentElement.style.cursor;
                //窗口左上角
                if (x > rect.left && x < rect.left + borderWidth && y > rect.top && y < rect.top + borderWidth) {
                    document.documentElement.style.cursor = "nw-resize";
                    this._isBorder = true;
                    this._wmszValue = 4;
                }
                //窗口左下角
                else if (x > rect.left && x < rect.left + borderWidth && y > rect.bottom - borderWidth && y < rect.bottom) {
                    document.documentElement.style.cursor = "sw-resize";
                    this._isBorder = true;
                    this._wmszValue = 7;
                }
                //窗口右上角
                else if (x > rect.right - borderWidth && x < rect.right && y > rect.top && y < rect.top + borderWidth) {
                    document.documentElement.style.cursor = "ne-resize";
                    this._isBorder = true;
                    this._wmszValue = 5;
                }
                //窗口右下角
                else if (x > rect.right - borderWidth && x < rect.right && y > rect.bottom - borderWidth && y < rect.bottom) {
                    document.documentElement.style.cursor = "se-resize";
                    this._isBorder = true;
                    this._wmszValue = 8;
                }
                //窗口左侧
                else if (x > rect.left && x < rect.left + borderWidth && y > rect.top + borderWidth && y < rect.bottom - borderWidth) {
                    document.documentElement.style.cursor = "e-resize";
                    this._isBorder = true;
                    this._wmszValue = 1;
                }
                //窗口右侧
                else if (x > rect.right - borderWidth && x < rect.right && y > rect.top + borderWidth && y < rect.bottom - borderWidth) {
                    document.documentElement.style.cursor = "w-resize";
                    this._isBorder = true;
                    this._wmszValue = 2;
                }
                //窗口上侧
                else if (x > rect.left + borderWidth && x < rect.right - borderWidth && y > rect.top && y < rect.top + borderWidth) {
                    document.documentElement.style.cursor = "n-resize";
                    this._isBorder = true;
                    this._wmszValue = 3;
                }
                //窗口下侧
                else if (x > rect.left + borderWidth && x < rect.right - borderWidth && y > rect.bottom - borderWidth && y < rect.bottom) {
                    document.documentElement.style.cursor = "s-resize";
                    this._isBorder = true;
                    console.log(window.event.button);
                    this._wmszValue = 6;
                }
                else {
                    if (this._isBorder)
                        document.documentElement.style.cursor = this._oldCursor;
                    this._isBorder = false;
                    this._wmszValue = 0;
                }
            }
            async OnChangeSize() {
                //console.log(this._wmszValue + ":" + window.event.button);
                if (window.event.button == 0 && this._wmszValue!=0)
                    await ThisWindow.OnChangeSize(this._wmszValue);
            }
        }
        let InitScript = async function () {
            let htmlElm = document.getElementsByTagName("html")[0];
            if (htmlElm.dataset.startSettings != undefined) {
                let startSettings = eval("(" + htmlElm.dataset.startSettings + ")");
                if (startSettings.Width != undefined)
                    await (ThisWindow.Width = startSettings.Width);
                if (startSettings.Height != undefined)
                    await (ThisWindow.Height = startSettings.Height);
                if (startSettings.SizeAble != undefined)
                    await (ThisWindow.SizeAble = startSettings.SizeAble);
                if (startSettings.Position != undefined)
                    await ThisWindow.SetPosition(startSettings.Position);
            }
            await ThisWindow.Show();

            let iconElms = document.getElementsByClassName("use-webapp-window-icon");
            for (let i = 0; i < iconElms.length; i++) {
                let icon = iconElms[i].dataset.windowIcon;
                if (icon != undefined) {
                    await (ThisWindow.Icon = icon);
                }
            }
            let captionElms = document.getElementsByClassName("use-webapp-window-caption");
            for (let i = 0; i < captionElms.length; i++) {
                captionElms[i].innerText = document.title;
            }
            let moveRegionElms = document.getElementsByClassName("use-webapp-window-move-region");
            for (let i = 0; i < moveRegionElms.length; i++) {
                moveRegionElms[i].addEventListener("mousedown", ThisWindow.OnMove);
            }
            let closeButtonElms = document.getElementsByClassName("use-webapp-window-close-button");
            for (let i = 0; i < closeButtonElms.length; i++) {
                closeButtonElms[i].addEventListener("mousedown", ThisWindow.Close);
            }
            let minimizButtonElms = document.getElementsByClassName("use-webapp-window-minimiz-button");
            for (let i = 0; i < minimizButtonElms.length; i++) {
                minimizButtonElms[i].addEventListener("mousedown", async function () { await (ThisWindow.WindowState = WindowState.Minimized); });
            }
            let maximizButtonElms = document.getElementsByClassName("use-webapp-window-maximiz-button");
            for (let i = 0; i < maximizButtonElms.length; i++) {
                maximizButtonElms[i].addEventListener("mousedown", async function () {
                    if (await (ThisWindow.WindowState) == WindowState.Maximized) {
                        await (ThisWindow.WindowState = WindowState.Normal);
                    }
                    else {
                        await (ThisWindow.WindowState = WindowState.Maximized);
                    }
                });
            }
            if (await (ThisWindow.BrowserType) == WebBrowserType.Edge) {
                window.addEventListener("mousemove", function (evt) { Tnelab.WebApp.BorderChecker.SetBorderRectCursor(); });
                window.addEventListener("mousedown", function (evt) { Tnelab.WebApp.BorderChecker.OnChangeSize(); });
            }
        }
        Tnelab.WebApp.BorderChecker = new _BorderChecker();
        Tnelab.WebApp.InitScript = InitScript;
    })(WebApp = Tnelab.WebApp || (Tnelab.WebApp = {}));
})(Tnelab || (Tnelab = {}));
document.addEventListener("DOMContentLoaded", Tnelab.WebApp.InitScript);
//document.write("<style>html{background-color:#ffffff;}</style>");